﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SFast
{
    public class MyUserLogin
    {
        #region 属性
        private string CookieName = "u";            //Cookie中用户的Key
        private string CookieAccount = "n";   //Cookie下用户登录名的key
        private string CookieUserName = "un";   //Cookie下用户名的key
        private string CookieUserID = "i";          //Cookie下用户ID的key
        private string CookieRoleName = "rn";          //Cookie下当前登录用户的角色名称的key
        private string CookieRoleID = "ri";          //Cookie下当前登录用户的角色ID的key


        /// <summary>
        /// 登录状态 true:登录 false:未登录
        /// </summary>
        public bool LoginState { get; set; }

        /// <summary>
        /// 登录用户名
        /// </summary>
        public string Account { get; set; }

        /// <summary>
        /// 登录者名称
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserID { get; set; }

        /// <summary>
        /// 登陆用户的角色
        /// </summary>
        public string RoleName { get; set; }

        /// <summary>
        /// 登录用户的角色ID
        /// </summary>
        public string RoleID { get; set; }

        #endregion

        #region 构造函数 并初始化
        /// <summary>
        /// 有参构造函数
        /// </summary>
        public MyUserLogin(string cookieName)
        {
            this.CookieName = cookieName;
            LoginCookie();
        }

        /// <summary>
        /// 无参构造函数
        /// </summary>
        public MyUserLogin()
        {
            this.CookieName = CookieName + HttpContext.Current.Request.Url.Port;//cookie名称加入端口号
            LoginCookie();
        }

        /// <summary>
        /// 初始化cookie
        /// </summary>
        private void LoginCookie()
        {
            var cookie = HttpContext.Current.Request.Cookies;

            //判断现在key=CookieName中有没有值
            if (cookie[CookieName] != null)
            {

                this.LoginState = true;
                this.Account = MyUrl.UrlDecode(cookie[CookieName][CookieAccount]);
                this.UserID = MyUrl.UrlDecode(cookie[CookieName][CookieUserID]);
                this.UserName = MyUrl.UrlDecode(cookie[CookieName][CookieUserName]);
                this.RoleName = MyUrl.UrlDecode(cookie[CookieName][CookieRoleName]);
                this.RoleID = MyUrl.UrlDecode(cookie[CookieName][CookieRoleID]);

            }
            else
            {
                this.LoginState = false;
            }
        }
        #endregion

        #region 用户登录成功后添加cookie
        /// <summary>
        /// 用户登录成功后添加cookie
        /// </summary>
        /// <param name="account">登录用户名</param>
        /// <param name="UserID">用户ID</param>
        public void Login(string account, string UserID)
        {
            if (!string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(UserID))
            {
                var cookie = HttpContext.Current.Response.Cookies[CookieName];

                this.LoginState = true;
                cookie[this.CookieAccount] = MyUrl.UrlEncode(account);
                cookie[this.CookieUserID] = MyUrl.UrlEncode(UserID);
            }
        }


        /// <summary>
        /// 用户登录成功后添加cookie(只针对一个用户一个角色状态)
        /// </summary>
        /// <param name="account">登录用户名</param>
        /// <param name="userID">用户ID</param>
        /// <param name="roleName">角色名称</param>
        /// <param name="roleID">角色ID</param>
        public void Login(string account, string userID,String roleName ,String roleID)
        {
            if (!string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(userID))
            {
                var cookie = HttpContext.Current.Response.Cookies[CookieName];

                this.LoginState = true;
                cookie[this.CookieAccount] = MyUrl.UrlEncode(account);
                cookie[this.CookieUserID] = MyUrl.UrlEncode(userID);
                cookie[CookieRoleID] = MyUrl.UrlEncode(roleID);
                cookie[CookieRoleName] = MyUrl.UrlEncode(roleName);
            }
        }
        #endregion

        #region 退出  删除cookie
        /// <summary>
        /// 退出
        /// </summary>
        public void Quit()
        {
            if (HttpContext.Current.Request.Cookies[CookieName] != null)
            {
                HttpCookie myCookie = HttpContext.Current.Request.Cookies[CookieName];
                myCookie.Expires = DateTime.Now.AddYears(-1);
                HttpContext.Current.Response.Cookies.Add(myCookie);
            }
        }

        #endregion
    }
}
